package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
@Slf4j
public class DishServicelmpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @Override
    public Result insertDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        //向菜品表插入1条数据
        dishMapper.insert(dish);//后绪步骤实现
        //获取insert语句生成的主键值
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);//后绪步骤实现

        }return Result.success();

    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByiId(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.getById(id);
        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors= dishFlavorMapper.getByDishId(id);//后绪步骤实现
        //将查询到的数据封装到VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
       return dishVO;

    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> page(DishPageQueryDTO dto) {
        if (dto.getPage()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        } if (dto.getPageSize()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper
        Page<DishVO> page = dishMapper.getPage(dto);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        //封装到PageResult中
        PageResult pageResult = new PageResult(total, result);
        return Result.success(pageResult);
    }

    /**
     * 根据分类id查询菜品数量
     *
     * @param categoryId
     * @return
     */
    @Override
    public Integer countByCategoryId(Long categoryId) {

        return setmealDishMapper.countByCategoryId(categoryId);
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateDish(DishDTO dto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteDishFlavor(Arrays.asList(dish.getId()));
        //3.新增菜品口味关联表
        List<DishFlavor> flavors = dto.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
        }
        //批量插入
        dishFlavorMapper.insertBatch(flavors);
        return Result.success();
    }

    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result delete(List<Long> ids) {
        //1.查询当前菜品是否在起售状态，如果是则不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                return Result.error(MessageConstant.DISH_ON_SALE);
            }
        }
        //2.查询当前菜品是否关联套餐，如果关联则不能删除
        List<Long> setmealDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealDishIds!=null && setmealDishIds.size()>0){
            return Result.error(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.如果删除了菜品的话，则关联的口味数据也需要删除掉
        dishMapper.deleteDish(ids);
        dishFlavorMapper.deleteDishFlavor(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)//set默认为1，启用
                .build();

        return dishMapper.list(dish);
    }

    /**
     * 启售状态
     *
     * @param status id
     * @param id
     * @return
     */
    @Override
    public Result addStatus(Integer status, Long id) {
        //判断非空
        if (status==null){
            return Result.error(MessageConstant.ACCOUNT_LOCKED);
        }
        if (id == null) {
            return Result.error(MessageConstant.ACCOUNT_NULL);
        }
        //调用mapper
        Dish dto=new Dish();
        dto.setStatus(status);
        dto.setId(id);
        dishMapper.update(dto);
        return Result.success();
    }
    /**
     * user
     */
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}